import {contextBridge, ipcRenderer} from 'electron'
import {TabInfo} from './core/TabManager';
import { StoreSchema } from './store';

// API类型定义
interface WindowAPI {
  createWindow: (url?: string) => Promise<number>;
  getWindowCount: () => Promise<number>;
  getCurrentWindowId: () => Promise<number | null>;
  saveWindowState: () => Promise<SavedWindowState | null>;
}

// 保存的窗口状态接口
interface SavedWindowState {
  x?: number;
  y?: number;
  width: number;
  height: number;
  isMaximized?: boolean;
}

interface TabAPI {
  createTab: (windowId: number, url: string) => Promise<number>;
  closeTab: (windowId: number, index: number) => Promise<boolean>;
  switchTab: (windowId: number, index: number) => Promise<boolean>;
  refreshTab: (windowId: number, index?: number) => Promise<boolean>;
  navigateTo: (windowId: number, index: number, url: string) => Promise<boolean>;
  goBack: (windowId: number, index: number) => Promise<boolean>;
  goForward: (windowId: number, index: number) => Promise<boolean>;
  getTabInfo: (windowId: number, index: number) => Promise<TabInfo | null>;
  getAllTabs: (windowId: number) => Promise<TabInfo[]>;
  
  // 新增休眠标签功能
  hibernateTab: (windowId: number, index: number) => Promise<boolean>;
  
  // 事件监听
  onTabUpdated: (callback: (tabInfo: TabInfo) => void) => () => void;
  onTabActivated: (callback: (tabId: number) => void) => () => void;
  onTabClosed: (callback: (tabId: number) => void) => () => void;
  onNewWindowRequest: (callback: (data: any) => void) => () => void;
}

// 暴露保护的API给渲染进程
contextBridge.exposeInMainWorld('elec', {
  // 窗口管理API
  window: {
    createWindow: (url?: string) => ipcRenderer.invoke('create-window', url),
    getWindowCount: () => ipcRenderer.invoke('get-window-count'),
    getCurrentWindowId: () => ipcRenderer.invoke('get-current-window-id'),
    saveWindowState: () => ipcRenderer.invoke('save-window-state'),
  } as WindowAPI,
  
  // 标签页管理API
  tab: {
    createTab: (windowId: number, url: string) => 
      ipcRenderer.invoke('create-tab', {windowId, url}),
    
    closeTab: (windowId: number, index: number) => 
      ipcRenderer.invoke('close-tab', {windowId, index}),
    
    switchTab: (windowId: number, index: number) => 
      ipcRenderer.invoke('switch-tab', {windowId, index}),
    
    refreshTab: (windowId: number, index?: number) => 
      ipcRenderer.invoke('refresh-tab', {windowId, index}),
    
    navigateTo: (windowId: number, index: number, url: string) => 
      ipcRenderer.invoke('navigate-to', {windowId, index, url}),
    
    goBack: (windowId: number, index: number) => 
      ipcRenderer.invoke('go-back', {windowId, index}),
    
    goForward: (windowId: number, index: number) => 
      ipcRenderer.invoke('go-forward', {windowId, index}),
    
    getTabInfo: (windowId: number, index: number) => 
      ipcRenderer.invoke('get-tab-info', {windowId, index}),
    
    getAllTabs: (windowId: number) => 
      ipcRenderer.invoke('get-all-tabs', {windowId}),
    
    hibernateTab: (windowId: number, index: number) => 
      ipcRenderer.invoke('hibernate-tab', {windowId, index}),
    
    // 事件监听工具
    onTabUpdated: (callback: (tabInfo: TabInfo) => void) => {
      const eventHandler = (_: any, tabInfo: TabInfo) => callback(tabInfo);
      ipcRenderer.on('tab-updated', eventHandler);
      return () => ipcRenderer.removeListener('tab-updated', eventHandler);
    },
    
    onTabActivated: (callback: (tabId: number) => void) => {
      const eventHandler = (_: any, tabId: number) => callback(tabId);
      ipcRenderer.on('tab-activated', eventHandler);
      return () => ipcRenderer.removeListener('tab-activated', eventHandler);
    },
    
    onTabClosed: (callback: (tabId: number) => void) => {
      const eventHandler = (_: any, tabId: number) => callback(tabId);
      ipcRenderer.on('tab-closed', eventHandler);
      return () => ipcRenderer.removeListener('tab-closed', eventHandler);
    },
    
    onNewWindowRequest: (callback: (data: any) => void) => {
      const eventHandler = (_: any, data: any) => callback(data);
      ipcRenderer.on('new-window-request', eventHandler);
      return () => ipcRenderer.removeListener('new-window-request', eventHandler);
    }
  } as TabAPI,
})

// Store API
contextBridge.exposeInMainWorld('storeAPI', {
  // 设置相关
  getSettings: () => ipcRenderer.invoke('get-settings'),
  updateSettings: (settings: any) => ipcRenderer.invoke('update-settings', settings),
  
  // 书签相关
  getBookmarks: () => ipcRenderer.invoke('get-bookmarks'),
  addBookmark: (bookmark: any) => ipcRenderer.invoke('add-bookmark', bookmark),
  deleteBookmark: (id: string) => ipcRenderer.invoke('delete-bookmark', id),
  
  // 历史记录相关
  getHistory: () => ipcRenderer.invoke('get-history'),
  addHistoryEntry: (entry: any) => ipcRenderer.invoke('add-history-entry', entry),
  clearHistory: (options?: { startTime?: number; endTime?: number }) => 
    ipcRenderer.invoke('clear-history', options || {}),
  
  // 会话相关
  getCurrentTabs: () => ipcRenderer.invoke('get-current-tabs'),
  updateCurrentTabs: (tabs: any[]) => ipcRenderer.invoke('update-current-tabs', tabs),
  getLastClosedTabs: () => ipcRenderer.invoke('get-last-closed-tabs'),
  addToLastClosedTabs: (tab: any) => ipcRenderer.invoke('add-to-last-closed-tabs', tab),
  restoreClosedTab: (index?: number) => ipcRenderer.invoke('restore-closed-tab', index),
});
